package edu.gatech.cs2340.group7.wheresmystuffapp.views;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import edu.gatech.cs2340.group7.wheresmystuffapp.R;
import edu.gatech.cs2340.group7.wheresmystuffapp.presenters.DisplaySearchResultsPresenter;

public class DisplaySearchResultsView extends Activity implements
		OnClickListener {

	private DisplaySearchResultsPresenter myPresenter;
	private TextView searchResults;
	private Button backButton;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_display_search_results);
		init();
		search();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_display_main_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent(this, UserMainMenuView.class);
		intent.setPackage(getPackageName());
		startActivity(intent);

	}

	/**
	 * initializes all of the views in the XML layout file, and sets the listeners for the buttons
	 */
	private void init() {
		backButton = (Button) findViewById(R.id.b_searchResultsBack);
		backButton.setOnClickListener(this);
		searchResults = (TextView) findViewById(R.id.search_results_display);

		myPresenter = new DisplaySearchResultsPresenter(this);
	}

	/**
	 * Tells the presenter to search for items that match the entered parameters
	 */
	private void search() {
		Intent intent = getIntent();
		String searchParams = intent.getStringExtra("Parameters");
		myPresenter.showSearchResults(searchParams);

	}

	/**
	 * Displays the search results
	 * @param searchList the list of items that match the search parameters
	 */
	public void setSearchResults(String searchList) {
		searchResults.setText(searchList);
	}

}
